home *** CD-ROM | disk | FTP | other *** search
- /*
- FileList 1.4
- "Utilities.c"
- */
-
- #include "Main.h"
- #include "Stack.h"
- #include "Search.h"
- #include "Utilities.h"
-
- /* ----- Convert character to upper case ------------------------------- */
-
- short toupper (register unsigned char c)
- {
- return ((c >= 'a') && (c <= 'z')) ? (c - ('a' - 'A')) : c;
- }
-
- /* ----- Compare strings ----------------------------------------------- */
-
- short StrCompare (
- register unsigned char *s1,
- register unsigned char *s2)
- {
- register short d, i, min;
- register short c1, c2;
-
- d = *s1 - *s2;
- min = (d < 0) ? *s1 : *s2;
- for (i = 1; i <= min; i++) {
- s1++;
- s2++;
- c1 = toupper(*s1);
- c2 = toupper(*s2);
- if (c1 != c2)
- return c1 - c2;
- }
- return d;
- }
-
- /* ----- Match strings ------------------------------------------------- */
-
- static Boolean Match (
- register Byte *s, /* Pascal string */
- register Byte *p) /* Array */
- {
- register short n = *s++; /* String length */
-
- while (n-- > 0)
- if (toupper(*s++) != toupper(*p++))
- return FALSE; /* No match */
- return TRUE; /* s matches p */
- }
-
- Boolean StrIncludes (
- register Byte *s, /* Pascal string */
- register Byte *p) /* Pascal string */
- {
- register short n = *p++ - *s;
-
- while (n-- >= 0)
- if (Match(s, p++))
- return TRUE;
- return FALSE;
- }
-
- Boolean StrEnds (
- register Byte *s, /* Pascal string */
- register Byte *p) /* Pascal string */
- {
- if (!*p || *s > *p)
- return FALSE;
- return Match(s, p + (*p - *s + 1));
- }
-
- Boolean StrBegins (
- register Byte *s, /* Pascal string */
- register Byte *p) /* Pascal string */
- {
- if (!*p || *s > *p)
- return FALSE;
- return Match(s, p + 1);
- }
-
- Boolean StrEquals (
- register Byte *s, /* Pascal string */
- register Byte *p) /* Pascal string */
- {
- if (*p != *s)
- return FALSE;
- return Match(s, p + 1);
- }
-
- /* ----- Fill memory --------------------------------------------------- */
-
- void FillMemory (
- register unsigned char *p,
- register long n,
- register unsigned char c)
- {
- while(n--)
- *p++ = c;
- }
-
- /* ----- Right justify string ------------------------------------------ */
-
- void RightJustify (
- register unsigned char *s,
- register short w,
- register unsigned char c)
- {
- register short d, n;
-
- if ((d = w - (n = *s)) > 0) {
- *s++ = w;
- BlockMove(s, s + d, (long)n);
- while(d--)
- *s++ = c;
- }
- }
-
- /* ----- Left justify string ------------------------------------------- */
-
- void LeftJustify (
- register unsigned char *s,
- register short w,
- register unsigned char c)
- {
- register short d, n;
-
- if ((d = w - (n = *s)) > 0) {
- *s++ = w;
- s += n;
- while(d--)
- *s++ = c;
- }
- }
-
- /* ----- Append string2 to string1 ------------------------------------- */
-
- void Append (
- register unsigned char *s1,
- register unsigned char *s2)
- {
- register short d, n, i;
-
- n = *s1;
- d = *s2;
- i = n + d - 255;
- if (i > 0)
- d -= i;
- *s1 = n + d;
- BlockMove(s2 + 1, s1 + n + 1, d);
- }
-
- /* ----- Convert integer to 2 digits ----------------------------------- */
-
- void str2 (
- register unsigned short i,
- register unsigned char *s)
- {
- register unsigned char str[7];
-
- NumToString((long)i, str);
- if (i < 10) {
- s[0] = '0';
- s[1] = str[1];
- } else {
- s[0] = str[1];
- s[1] = str[2];
- }
- }
-
- /* ----- Convert date and time to string ------------------------------- */
-
- long date2str (
- long sec,
- register unsigned char *s,
- char sep)
- {
- register unsigned char *p;
- short date[7];
-
- Secs2Date(sec, &date);
- date[0] -= 1900;
- p = s;
- str2(date[DateFormat[0]], s); s += 2; *s++ = '/';
- str2(date[DateFormat[1]], s); s += 2; *s++ = '/';
- str2(date[DateFormat[2]], s); s += 2; *s++ = sep;
- str2(date[3], s); s += 2; *s++ = ':';
- str2(date[4], s); s += 2; *s++ = ':';
- str2(date[5], s); s += 2;
- return s-p;
- }
-
- /* ----- Make sure string is printable --------------------------------- */
-
- void printable (
- register unsigned char *p,
- register short n)
- {
- register unsigned char c;
-
- while (n--) {
- c = *p;
- if (c < 0x20 || c == 0x7F)
- *p = 0xC9;
- ++p;
- }
- }
-
- /* ----- Convert volume info to string (spaces) ------------------------ */
-
- void VolumeToString1 (
- unsigned long i,
- register unsigned char *s)
- {
- register FileInfoPtr p;
- register unsigned char n[10];
- unsigned char *t;
-
- if (i >= VolumeData.count) {
- s[0] = 0;
- return;
- }
- FillMemory(s, 256L, ' ');
- p = Address(&VolumeData, i);
- t = s;
- s++; /* Skip length byte */
-
- /* Folder name */
- BlockMove(p->name + 1, s, (long)(p->name[0]));
- printable(s, p->name[0]);
- s += 28;
-
- /* Available space */
- NumToString(p->type, n);
- BlockMove(n+1, s+(9-n[0]), (long)n[0]);
- s += 10;
-
- /* Total space */
- NumToString(p->size, n);
- BlockMove(n+1, s+(9-n[0]), (long)n[0]);
- s += 10;
-
- /* Number of files */
- NumToString(p->creator, n);
- BlockMove(n+1, s+(6-n[0]), (long)n[0]);
- s += 7;
-
- /* Dates */
- s += date2str(p->cdate, s, ' ') + 1;
- s += date2str(p->mdate, s, ' ');
-
- *t = s - t - 1; /* String length */
- }
-
- /* ----- Convert volume info to string (tabs) -------------------------- */
-
- void VolumeToString2 (
- unsigned long i,
- register unsigned char *s)
- {
- register FileInfoPtr p;
- register unsigned char n[10];
- register short length;
- unsigned char *t;
-
- if (i >= VolumeData.count) {
- s[0] = 0;
- return;
- }
- FillMemory(s, 256L, '\t');
- p = Address(&VolumeData, i);
- t = s;
- s++; /* Skip length byte */
-
- /* Folder name */
- BlockMove(p->name + 1, s, (long)(length = p->name[0]));
- printable(s, length);
- s += length+1;
-
- /* Available space */
- NumToString(p->type, n);
- BlockMove(n+1, s, (long)(length = n[0]));
- s += length+1;
-
- /* Total space */
- NumToString(p->size, n);
- BlockMove(n+1, s, (long)(length = n[0]));
- s += length+1;
-
- /* Number of files */
- NumToString(p->creator, n);
- BlockMove(n+1, s, (long)(length = n[0]));
- s += length+1;
-
- /* Dates */
- s += date2str(p->cdate, s, '\t') + 1;
- s += date2str(p->mdate, s, '\t');
-
- *t = s - t - 1; /* String length */
- }
-
- /* ----- Convert file info to string (spaces) -------------------------- */
-
- void FileToString1 (
- unsigned long i,
- register unsigned char *s)
- {
- register FileInfoPtr p;
- register unsigned char *t;
- register short length;
- unsigned char n[10];
- unsigned char *path;
- long x;
- STACK stack;
-
- if (i >= FileData.count) {
- s[0] = 0;
- return;
- }
- FillMemory(s, 256L, ' ');
- p = Address(&FileData, i);
- t = s;
- s++; /* Skip length byte */
-
- /* File name */
- BlockMove(p->name + 1, s, (long)(p->name[0]));
- printable(s, p->name[0]);
- s += 32;
-
- /* Type */
- x = p->type;
- printable((unsigned char *)&x, sizeof(long));
- BlockMove(&x, s, sizeof(long));
- s += 5;
-
- /* Creator */
- x = p->creator;
- printable((unsigned char *)&x, sizeof(long));
- BlockMove(&x, s, sizeof(long));
- s += 5;
-
- /* Size */
- NumToString(p->size, n);
- BlockMove(n+1, s+(7-n[0]), (long)n[0]);
- s += 8;
-
- /* Dates */
- s += date2str(p->cdate, s, ' ') + 1;
- s += date2str(p->mdate, s, ' ') + 1;
-
- /* Volume name and path */
- InitPath(p, &stack);
- length = 254 - (s - t - 1);
- while ((path = NextPath(&stack)) && (*path <= length)) {
- BlockMove(path+1, s, (long)(*path));
- printable(s, *path);
- s += *path;
- *s++ = ':';
- length -= *path + 1;
- }
-
- *t = s - t - 1; /* String length */
- }
-
- /* ----- Convert file info to string (tabs) ---------------------------- */
-
- void FileToString2 (
- unsigned long i,
- register unsigned char *s)
- {
- register FileInfoPtr p;
- register unsigned char *t;
- register short length;
- unsigned char n[10];
- unsigned char *path;
- long x;
- STACK stack;
-
- if (i >= FileData.count) {
- s[0] = 0;
- return;
- }
- FillMemory(s, 256L, '\t');
- p = Address(&FileData, i);
- t = s;
- s++; /* Skip length byte */
-
- /* File name */
- BlockMove(p->name + 1, s, (long)(length = p->name[0]));
- printable(s, length);
- s += length+1;
-
- /* Type */
- x = p->type;
- printable((unsigned char *)&x, sizeof(long));
- BlockMove(&x, s, sizeof(long));
- s += 5;
-
- /* Creator */
- x = p->creator;
- printable((unsigned char *)&x, sizeof(long));
- BlockMove(&x, s, sizeof(long));
- s += 5;
-
- /* Size */
- NumToString(p->size, n);
- BlockMove(n+1, s, (long)(length = n[0]));
- s += length+1;
-
- /* Dates */
- s += date2str(p->cdate, s, '\t') + 1;
- s += date2str(p->mdate, s, '\t') + 1;
-
- /* Volume name and path */
- InitPath(p, &stack);
- path = NextPath(&stack);
- BlockMove(path+1, s, (long)(*path));
- s += *path + 1;
- length = 254 - (s - t - 1);
- while ((path = NextPath(&stack)) && (*path <= length)) {
- BlockMove(path+1, s, (long)(*path));
- printable(s, *path);
- s += *path;
- *s++ = ':';
- length -= *path + 1;
- }
-
- *t = s - t - 1; /* String length */
- }
-
- /* ----- Center dialog or alert template ------------------------------- */
-
- void CenterDialog (
- long templateType, /* 'ALRT' or 'DLOG' */
- short templateID)
- {
- short screenWidth = Bounds.right - Bounds.left;
- short screenHeight = Bounds.bottom - Bounds.top;
- register Rect **h; /* Templates start with boundsRect */
- register Rect *p;
- register short width, height;
-
- if (!(h = (Rect **)GetResource(templateType, templateID)))
- return;
- p = *h;
- width = p->right - p->left;
- height = p->bottom - p->top;
- p->top = Bounds.top + (screenHeight - height)/3;
- p->bottom = p->top + height;
- p->left = Bounds.left + (screenWidth - width)/2;
- p->right = p->left + width;
- }
-
- /* ----- Where to put a dialog box so that it's centered --------------- */
-
- void GetDlogOrigin (
- short dlogID,
- register Point *where)
- {
- short screenWidth = Bounds.right - Bounds.left;
- short screenHeight = Bounds.bottom - Bounds.top;
- register DialogTHndl dlogHandle;
- register Rect *drp;
- register short dlogWidth, dlogHeight;
-
- dlogHandle = (DialogTHndl)GetResource('DLOG', dlogID);
- if (!dlogHandle){
- SetPt(where, 85, 85);
- return;
- }
- drp = &((**dlogHandle).boundsRect);
- dlogWidth = drp->right - drp->left;
- dlogHeight = drp->bottom - drp->top;
- where->h = Bounds.left + (screenWidth - dlogWidth)/2;
- where->v = Bounds.top + (screenHeight - dlogHeight)/3;
- }
-
- /* ----- Set control name ---------------------------------------------- */
-
- void ControlName (
- register DialogPtr dialog,
- register short itemNo,
- register unsigned char *name)
- {
- short type;
- ControlHandle control;
- Rect box;
-
- GetDItem(dialog, itemNo, &type, &control, &box);
- SetCTitle(control, name);
- if (*name)
- /*HiliteControl(control, 0);*/
- ShowControl(control);
- else
- /*HiliteControl(control, 255);*/
- HideControl(control);
- }
-
- /* ----- Get control value --------------------------------------------- */
-
- short ControlCheck (
- register DialogPtr dialog,
- register short itemNo)
- {
- short type;
- ControlHandle control;
- Rect box;
-
- GetDItem(dialog, itemNo, &type, &control, &box);
- return GetCtlValue(control);
- }
-
- /* ----- Set control value --------------------------------------------- */
-
- void ControlSet (
- register DialogPtr dialog,
- register short itemNo,
- register short value)
- {
- short type;
- ControlHandle control;
- Rect box;
-
- GetDItem(dialog, itemNo, &type, &control, &box);
- SetCtlValue(control, value);
- }
-
- /* ----- Toggle control value ------------------------------------------ */
-
- void ControlToggle (
- register DialogPtr dialog,
- register short itemNo)
- {
- short type;
- ControlHandle control;
- Rect box;
-
- GetDItem(dialog, itemNo, &type, &control, &box);
- SetCtlValue(control, !GetCtlValue(control));
- }
-
- /* ----- Set group of radio buttons ------------------------------------ */
-
- Boolean SetRadioButton (
- register DialogPtr dialog,
- register short item1,
- register short item2,
- register short item)
- {
- register short i;
- short type;
- Handle itemHdl;
- Rect box;
-
- if (item < item1 || item > item2)
- return FALSE;
- for (i = item1; i <= item2; i++) {
- GetDItem(dialog, i, &type, &itemHdl, &box);
- SetCtlValue((ControlHandle)itemHdl, (item == i) ? 1 : 0);
- }
- return TRUE;
- }
-
- /* ----- Get group of radio buttons ------------------------------------ */
-
- short GetRadioButton (
- register DialogPtr dialog,
- register short item1,
- register short item2)
- {
- register short i;
- short type;
- Handle itemHdl;
- Rect box;
-
- for (i = item1; i <= item2; i++) {
- GetDItem(dialog, i, &type, &itemHdl, &box);
- if (GetCtlValue((ControlHandle)itemHdl))
- return i;
- }
- return 0;
- }
-
- /* ----- Get dialog item text ------------------------------------------ */
-
- void getText (
- register DialogPtr dPtr,
- short item,
- unsigned char *str)
- {
- short theType;
- Handle theItem;
- Rect theBox;
-
- GetDItem(dPtr, item, &theType, &theItem, &theBox);
- GetIText(theItem, str);
- }
-
- /* ----- Set dialog item text ------------------------------------------ */
-
- void setText (
- register DialogPtr dialogP,
- short id,
- unsigned char *s)
- {
- short item;
- Handle itemHdl;
- Rect box;
-
- GetDItem(dialogP, id, &item, &itemHdl, &box);
- SetIText(itemHdl, s);
- }
-
- /* ----- Set dialog item number ---------------------------------------- */
-
- void setNumber (
- register DialogPtr dialogP,
- short id,
- long n)
- {
- unsigned char s[10];
-
- NumToString(n, s);
- setText(dialogP, id, s);
- }
-
- /* ----- Get dialog item number ---------------------------------------- */
-
- long getNumber (
- register DialogPtr dialogP,
- short id,
- register long min,
- register long max)
- {
- long n;
- register unsigned char s[256];
-
- getText(dialogP, id, s);
- StringToNum(s, &n);
- if (n < min)
- n = min;
- else
- if (n > max)
- n = max;
- return n;
- }
-
- /* ----- Show/hide dialog item ----------------------------------------- */
-
- void ShowHideControl (
- register DialogPtr dialog,
- register short i,
- register Boolean show)
- {
- short type;
- ControlHandle control;
- Rect box;
-
- GetDItem(dialog, i, &type, &control, &box);
- if (show)
- ShowControl(control);
- else
- HideControl(control);
- }
-
- /* ----- Draw user item in dialog box ---------------------------------- */
-
- pascal void DrawICN (
- register DialogPtr dialogP,
- register short id)
- {
- register Handle h;
- short item;
- Handle itemHdl;
- Rect box;
-
- GetDItem(dialogP, id, &item, &itemHdl, &box);
- if (h = GetResource('ICN#', 128)) {
- HLock(h);
- PlotIcon(&box, h);
- HUnlock(h);
- }
- }
-
- /* ----- Frame item in dialog ------------------------------------------ */
-
- pascal void FrameItem (
- register DialogPtr window,
- register short number)
- {
- short type;
- Handle item;
- Rect box;
-
- GetDItem(window, number, &type, &item, &box);
- FrameRect(&box);
- MoveTo(box.left + 3, box.bottom);
- LineTo(box.right, box.bottom );
- LineTo(box.right, box.top + 3);
- }
-
- /* ----- Set user item procedure --------------------------------------- */
-
- void SetUserItem (
- register DialogPtr dialog,
- register short number,
- register ProcPtr proc)
- {
- short type;
- Handle item;
- Rect box;
-
- GetDItem(dialog, number, &type, &item, &box);
- SetDItem(dialog, number, userItem, (Handle)proc, &box);
- }
-
- /* ----- SF Dialog Hook to outline default button ---------------------- */
-
- pascal short SFOutlineHook (
- register short itemno,
- register DialogPtr dialog)
- {
- short type;
- ControlHandle item;
- Rect box;
- unsigned char str[256];
-
- if (itemno == -1) {
- /* Append '@' to name of Open/Save button */
- GetDItem(dialog, getOpen, &type, &item, &box);
- GetCTitle(item, str);
- Append(str, (unsigned char *)"\p@");
- SetCTitle(item, str);
- }
- return itemno;
- }
-
- /* ----- Convert date formats ------------------------------------------ */
-
- static short Dates[][3] = {
- { 2, 1, 0 }, /* day/month/year */
- { 1, 2, 0 }, /* month/day/year */
- { 0, 1, 2 }, /* year/month/day */
- { 0, 0, 0 },
- };
-
- void DFormat1 (register short n)
- {
- register short i;
-
- for (i = 0; i < 3; i++)
- DateFormat[i] = Dates[n][i];
- }
-
- short DFormat2 (void)
- {
- register short i, n;
-
- for (n = 0; n < 3; n++) {
- for (i = 0; i < 3; i++)
- if (DateFormat[i] != Dates[n][i])
- break;
- if (i == 3)
- return n;
- }
- return n;
- }
-
- /* ----- Set watch cursor ---------------------------------------------- */
-
- void SetWatch (void)
- {
- RomMapInsert = mapTrue;
- SetCursor(*GetCursor(watchCursor)); /* Should be in ROM */
- }
-